package cn.tedu.sp04.order.service;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.order.feignclient.ItemFeignClient;
import cn.tedu.sp04.order.feignclient.UserFeignClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ItemFeignClient itemFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public Order getOrder(String orderId) {

        //通过springCloud远程调用，调用itemService，通过orderId获取items信息
        JsonResult<List<Item>> items =  itemFeignClient.getItems(orderId);

        //通过springCloud远程调用，调用userService，我规定查userId为7的用户，获取user信息
        JsonResult<User> user = userFeignClient.getUser(7);
        //根据订单id 查询到订单信息
        Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData());
        order.setItems(items.getData());

        return order;
    }

    @Override
    public void addOrder(Order order) {
        //调用itemService减少item库存
        itemFeignClient.decreaseNumber(order.getItems());

        //调用userService增加用户积分
        userFeignClient.addScore(7,888);
        //将传过来的order 加入数据库
        log.info("保存订单："+order);

    }
}
